<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    

    

    



    <meta charset="utf-8">
    
    
    <meta name="sogou_site_verification" content="true">
    
    
    
    <title>我在github上的Java项目 | Lvshen&#39;s Blog | This is My World</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="Java,github">
    <meta name="baidu-site-verification" content="VIVNdSiMZm">
    <meta name="description" content="github地址：Lvshen项目-Demo[Java技术栈]  模块一：自定义注解com.lvshen.demo.annotation注解定义123456789@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.FIELD)public @interface NeedSetValue &amp;#123;    Class&amp;lt;?&amp;gt; b">
<meta name="keywords" content="Java,github">
<meta property="og:type" content="article">
<meta property="og:title" content="我在github上的Java项目">
<meta property="og:url" content="https://lvshen9.gitee.io/2020/05/24/1/index.html">
<meta property="og:site_name" content="Lvshen&#39;s Blog">
<meta property="og:description" content="github地址：Lvshen项目-Demo[Java技术栈]  模块一：自定义注解com.lvshen.demo.annotation注解定义123456789@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.FIELD)public @interface NeedSetValue &amp;#123;    Class&amp;lt;?&amp;gt; b">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2020-05-26T05:46:08.926Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="我在github上的Java项目">
<meta name="twitter:description" content="github地址：Lvshen项目-Demo[Java技术栈]  模块一：自定义注解com.lvshen.demo.annotation注解定义123456789@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.FIELD)public @interface NeedSetValue &amp;#123;    Class&amp;lt;?&amp;gt; b">
    
    <link rel="shortcut icon" href="/img/mylogo.jpg">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">我的技术小房间</h5>
          <a href="mailto:https://lvshen9.github.io" title="https://lvshen9.github.io" class="mail">https://lvshen9.github.io</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/about"  >
                <i class="icon icon-lg icon-address-book"></i>
                About
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/collection"  >
                <i class="icon icon-lg icon-apple"></i>
                Collection
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://lvshen9.github.io/" target="_blank" >
                <i class="icon icon-lg icon-wordpress"></i>
                Blog
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/lvshen9" target="_blank" >
                <i class="icon icon-lg icon-github-alt"></i>
                GitHub
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">我在github上的Java项目</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">我在github上的Java项目</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-05-24T05:42:35.000Z" itemprop="datePublished" class="page-time">
  2020-05-24
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/技术/">技术</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#模块一：自定义注解com-lvshen-demo-annotation"><span class="post-toc-number">1.</span> <span class="post-toc-text">模块一：自定义注解com.lvshen.demo.annotation</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#注解定义"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">注解定义</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#切面编程实现注解功能"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">切面编程实现注解功能</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#使用注解"><span class="post-toc-number">1.3.</span> <span class="post-toc-text">使用注解</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目二：算法"><span class="post-toc-number">2.</span> <span class="post-toc-text">项目二：算法</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#求根号2的值"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">求根号2的值</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#队列"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">队列</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#抽奖"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">抽奖</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#最大公约数"><span class="post-toc-number">2.4.</span> <span class="post-toc-text">最大公约数</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#队列解密"><span class="post-toc-number">2.5.</span> <span class="post-toc-text">队列解密</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#搜索算法"><span class="post-toc-number">2.6.</span> <span class="post-toc-text">搜索算法</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#长链转短链"><span class="post-toc-number">2.7.</span> <span class="post-toc-text">长链转短链</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#雪花算法"><span class="post-toc-number">2.8.</span> <span class="post-toc-text">雪花算法</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#排序算法"><span class="post-toc-number">2.9.</span> <span class="post-toc-text">排序算法</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#回文数判断算法"><span class="post-toc-number">2.10.</span> <span class="post-toc-text">回文数判断算法</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#订单编号生成算法"><span class="post-toc-number">2.11.</span> <span class="post-toc-text">订单编号生成算法</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目三：幂等注解"><span class="post-toc-number">3.</span> <span class="post-toc-text">项目三：幂等注解</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目四：Java基础"><span class="post-toc-number">4.</span> <span class="post-toc-text">项目四：Java基础</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#BigDecimal"><span class="post-toc-number">4.1.</span> <span class="post-toc-text">BigDecimal</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#CAS"><span class="post-toc-number">4.2.</span> <span class="post-toc-text">CAS</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#volatile"><span class="post-toc-number">4.3.</span> <span class="post-toc-text">volatile</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#Date"><span class="post-toc-number">4.4.</span> <span class="post-toc-text">Date</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目五：设计模式com-lvshen-demo-design"><span class="post-toc-number">5.</span> <span class="post-toc-text">项目五：设计模式com.lvshen.demo.design</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目六：Zookeeper分布式锁com-lvshen-demo-distributelock"><span class="post-toc-number">6.</span> <span class="post-toc-text">项目六：Zookeeper分布式锁com.lvshen.demo.distributelock</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目七：excel导出com-lvshen-demo-export"><span class="post-toc-number">7.</span> <span class="post-toc-text">项目七：excel导出com.lvshen.demo.export</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目八：Guava使用com-lvshen-demo-guava-study"><span class="post-toc-number">8.</span> <span class="post-toc-text">项目八：Guava使用com.lvshen.demo.guava.study</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目九：kafka使用com-lvshen-demo-kafka"><span class="post-toc-number">9.</span> <span class="post-toc-text">项目九：kafka使用com.lvshen.demo.kafka</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目十：spring-websocket使用com-lvshen-demo-kafka；"><span class="post-toc-number">10.</span> <span class="post-toc-text">项目十：spring-websocket使用com.lvshen.demo.kafka；</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目十一：手写Lock-com-lvshen-demo-lock；"><span class="post-toc-number">11.</span> <span class="post-toc-text">项目十一：手写Lock  com.lvshen.demo.lock；</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目十二：mybatis开发-com-lvshen-demo-member；"><span class="post-toc-number">12.</span> <span class="post-toc-text">项目十二：mybatis开发 com.lvshen.demo.member；</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#entity"><span class="post-toc-number">12.1.</span> <span class="post-toc-text">entity</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#mapper"><span class="post-toc-number">12.2.</span> <span class="post-toc-text">mapper</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#service"><span class="post-toc-number">12.3.</span> <span class="post-toc-text">service</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#controller"><span class="post-toc-number">12.4.</span> <span class="post-toc-text">controller</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#Mapper-xml"><span class="post-toc-number">12.5.</span> <span class="post-toc-text">Mapper.xml</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目十三：redis开发-com-lvshen-demo-redis；"><span class="post-toc-number">13.</span> <span class="post-toc-text">项目十三：redis开发 com.lvshen.demo.redis；</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#分布式缓存"><span class="post-toc-number">13.1.</span> <span class="post-toc-text">分布式缓存</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#分布式session"><span class="post-toc-number">13.2.</span> <span class="post-toc-text">分布式session</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#发布与订阅"><span class="post-toc-number">13.3.</span> <span class="post-toc-text">发布与订阅</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#延迟队列与限流实现"><span class="post-toc-number">13.4.</span> <span class="post-toc-text">延迟队列与限流实现</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#可重入锁"><span class="post-toc-number">13.5.</span> <span class="post-toc-text">可重入锁</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目十四：NIO-demo-com-lvshen-demo-nio；"><span class="post-toc-number">14.</span> <span class="post-toc-text">项目十四：NIO demo com.lvshen.demo.nio；</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目十五：Postfix-Completion-使用-com-lvshen-demo-postfixcompletion；"><span class="post-toc-number">15.</span> <span class="post-toc-text">项目十五：Postfix Completion 使用 com.lvshen.demo.postfixcompletion；</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目十六：手写Spring启动流程-com-lvshen-demo-spring；"><span class="post-toc-number">16.</span> <span class="post-toc-text">项目十六：手写Spring启动流程 com.lvshen.demo.spring；</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目十七：-SpringCloud-Alibaba使用-com-lvshen-demo-springcloudalibaba；"><span class="post-toc-number">17.</span> <span class="post-toc-text">项目十七： SpringCloud Alibaba使用 com.lvshen.demo.springcloudalibaba；</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目十八：面试题-com-lvshen-demo-test；"><span class="post-toc-number">18.</span> <span class="post-toc-text">项目十八：面试题 com.lvshen.demo.test；</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#阿里面试题"><span class="post-toc-number">18.1.</span> <span class="post-toc-text">阿里面试题</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#华为面试题"><span class="post-toc-number">18.2.</span> <span class="post-toc-text">华为面试题</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#腾讯面试题"><span class="post-toc-number">18.3.</span> <span class="post-toc-text">腾讯面试题</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目十九：Thread多线程com-lvshen-demo-test；"><span class="post-toc-number">19.</span> <span class="post-toc-text">项目十九：Thread多线程com.lvshen.demo.test；</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#AQS"><span class="post-toc-number">19.1.</span> <span class="post-toc-text">AQS</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#Semaphone"><span class="post-toc-number">19.1.1.</span> <span class="post-toc-text">Semaphone</span></a></li><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#CountDownLatch"><span class="post-toc-number">19.1.2.</span> <span class="post-toc-text">CountDownLatch</span></a></li><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#CyclicBarrier"><span class="post-toc-number">19.1.3.</span> <span class="post-toc-text">CyclicBarrier</span></a></li></ol></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#线程状态"><span class="post-toc-number">19.2.</span> <span class="post-toc-text">线程状态</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#sleep"><span class="post-toc-number">19.3.</span> <span class="post-toc-text">sleep</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#join"><span class="post-toc-number">19.4.</span> <span class="post-toc-text">join</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#synchronized"><span class="post-toc-number">19.5.</span> <span class="post-toc-text">synchronized</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#wait"><span class="post-toc-number">19.6.</span> <span class="post-toc-text">wait</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#park"><span class="post-toc-number">19.7.</span> <span class="post-toc-text">park</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#手写future类"><span class="post-toc-number">19.8.</span> <span class="post-toc-text">手写future类</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#LockSupport使用"><span class="post-toc-number">19.9.</span> <span class="post-toc-text">LockSupport使用</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目二十：-本地线程-com-lvshen-demo-threadlocal；"><span class="post-toc-number">20.</span> <span class="post-toc-text">项目二十： 本地线程 com.lvshen.demo.threadlocal；</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目二十一：手写线程池-com-lvshen-demo-threadpool；"><span class="post-toc-number">21.</span> <span class="post-toc-text">项目二十一：手写线程池 com.lvshen.demo.threadpool；</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目二十二：递归实现树状结构-com-lvshen-demo-treenode；"><span class="post-toc-number">22.</span> <span class="post-toc-text">项目二十二：递归实现树状结构 com.lvshen.demo.treenode；</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目二十三：Java8使用-com-lvshen-demo-yjh-java8；"><span class="post-toc-number">23.</span> <span class="post-toc-text">项目二十三：Java8使用 com.lvshen.demo.yjh.java8；</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目二十四：Zookeeper监听机制使用-com-lvshen-demo-zookeeperdemo；"><span class="post-toc-number">24.</span> <span class="post-toc-text">项目二十四：Zookeeper监听机制使用 com.lvshen.demo.zookeeperdemo；</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#项目二十五：测试类-test-java-com-lvshen-demo；"><span class="post-toc-number">25.</span> <span class="post-toc-text">项目二十五：测试类 test.java.com.lvshen.demo；</span></a></li></ol>
        </nav>
    </aside>


<article id="post-1"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">我在github上的Java项目</h1>
        <div class="post-meta">
            <time class="post-time" title="2020-05-24 13:42:35" datetime="2020-05-24T05:42:35.000Z"  itemprop="datePublished">2020-05-24</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/技术/">技术</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <blockquote>
<p>github地址：<a href="https://github.com/lvshen9/demo" target="_blank" rel="noopener">Lvshen项目-Demo[Java技术栈]</a></p>
</blockquote>
<h4 id="模块一：自定义注解com-lvshen-demo-annotation"><a href="#模块一：自定义注解com-lvshen-demo-annotation" class="headerlink" title="模块一：自定义注解com.lvshen.demo.annotation"></a>模块一：自定义注解<code>com.lvshen.demo.annotation</code></h4><h5 id="注解定义"><a href="#注解定义" class="headerlink" title="注解定义"></a>注解定义</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Retention</span>(RetentionPolicy.RUNTIME)</span><br><span class="line"><span class="meta">@Target</span>(ElementType.FIELD)</span><br><span class="line"><span class="keyword">public</span> <span class="meta">@interface</span> NeedSetValue &#123;</span><br><span class="line">    Class&lt;?&gt; beanClass();</span><br><span class="line">    <span class="function">String <span class="title">param</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="function">String <span class="title">method</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="function">String <span class="title">targetFiled</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h5 id="切面编程实现注解功能"><a href="#切面编程实现注解功能" class="headerlink" title="切面编程实现注解功能"></a>切面编程实现注解功能</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SetFieldValueAspect</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    BeanUtil beanUtil;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Around</span>(<span class="string">"@annotation(com.lvshen.demo.annotation.NeedSetValueField)"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">doSetFieldValue</span><span class="params">(ProceedingJoinPoint point)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">        Object o = point.proceed();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (o <span class="keyword">instanceof</span> Collection) &#123;</span><br><span class="line">            <span class="keyword">this</span>.beanUtil.setFieldValueForCol(((Collection) o));</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">//返回结果不是集合的逻辑</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> o;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="使用注解"><a href="#使用注解" class="headerlink" title="使用注解"></a>使用注解</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@NeedSetValueField</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> List&lt;Member&gt; <span class="title">listMemberVO</span><span class="params">(List&lt;Integer&gt; codes)</span> </span>&#123;</span><br><span class="line">    List&lt;Member&gt; members = Lists.newArrayList();</span><br><span class="line">    <span class="keyword">for</span> (Integer code : codes) &#123;</span><br><span class="line">        Member memberByCode = getMemberByCode(code);</span><br><span class="line">        members.add(memberByCode);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> members;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目二：算法"><a href="#项目二：算法" class="headerlink" title="项目二：算法"></a>项目二：算法</h4><h5 id="求根号2的值"><a href="#求根号2的值" class="headerlink" title="求根号2的值"></a>求根号2的值</h5><p>收录了阿里面试题<code>com.lvshen.demo.arithmetic.alibabatest</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Description:面试题002 已知sqrt (2)约等于1.414，要求不用数学库，求sqrt (2)精确到小数点后10位。</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Lvshen</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>: 2020/4/24 17:40</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@since</span> JDK 1.8</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test1</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">double</span> EPSINON = <span class="number">0.0000000001</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">sqrt2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">double</span> low = <span class="number">1.4</span>;</span><br><span class="line">        <span class="keyword">double</span> high = <span class="number">1.5</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">double</span> mid = (low + high) / <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (high - low &gt; EPSINON) &#123;</span><br><span class="line">            <span class="keyword">if</span> ((mid * mid &gt; <span class="number">2</span>)) &#123;</span><br><span class="line">                high = mid;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                low = mid;</span><br><span class="line">            &#125;</span><br><span class="line">            mid = (high + low) / <span class="number">2</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> mid;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        System.out.println(sqrt2());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="队列"><a href="#队列" class="headerlink" title="队列"></a>队列</h5><p>JDK的队列<code>com.lvshen.demo.arithmetic.deque</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    Deque&lt;String&gt; arrayDeque = <span class="keyword">new</span> ArrayDeque&lt;&gt;();</span><br><span class="line">    <span class="comment">//arrayDeque.addLast("t1");</span></span><br><span class="line">    arrayDeque.addFirst(<span class="string">"t2"</span>);</span><br><span class="line">    arrayDeque.addFirst(<span class="string">"t3"</span>);</span><br><span class="line">    arrayDeque.addFirst(<span class="string">"t4"</span>);</span><br><span class="line">    arrayDeque.addFirst(<span class="string">"t5"</span>);</span><br><span class="line">    <span class="comment">//arrayDeque.addLast("t6");</span></span><br><span class="line"></span><br><span class="line">    log.info(arrayDeque.toString());</span><br><span class="line">    <span class="comment">//String pop = arrayDeque.pop();</span></span><br><span class="line">    String removeLast = arrayDeque.removeLast();</span><br><span class="line">    log.info(arrayDeque.toString());</span><br><span class="line">    log.info(removeLast);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="抽奖"><a href="#抽奖" class="headerlink" title="抽奖"></a>抽奖</h5><p>抽奖算法<code>com.lvshen.demo.arithmetic.lottery</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**整体思想： 奖品集合 + 概率比例集合 将奖品按集合中顺序概率计算成所占比例区间，放入比例集合。并产生一个随机数   *加入其中，排序。 排序后，随机数落在哪个区间，就表示那个区间的奖品被抽中。</span></span><br><span class="line"><span class="comment">  *返回的随机数在集合中的索引，该索引就是奖品集合中的索引。 比例区间的计算通过概率相加获得。</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">draw</span><span class="params">(List&lt;Double&gt; giftProbList)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">   List&lt;Double&gt; sortRateList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line"></span><br><span class="line">   <span class="comment">// 计算概率总和</span></span><br><span class="line">   Double sumRate = <span class="number">0</span>D;</span><br><span class="line">   <span class="keyword">for</span> (Double prob : giftProbList) &#123;</span><br><span class="line">      sumRate += prob;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">if</span> (sumRate != <span class="number">0</span>) &#123;</span><br><span class="line">      <span class="keyword">double</span> rate = <span class="number">0</span>D; <span class="comment">// 概率所占比例</span></span><br><span class="line">      <span class="keyword">for</span> (Double prob : giftProbList) &#123;</span><br><span class="line">         rate += prob;</span><br><span class="line">         <span class="comment">// 构建一个比例区段组成的集合(避免概率和不为1)</span></span><br><span class="line">         sortRateList.add(rate / sumRate);</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 随机生成一个随机数，并排序</span></span><br><span class="line">      <span class="comment">//double random = Math.random();</span></span><br><span class="line">           ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();</span><br><span class="line">           <span class="keyword">double</span> random = threadLocalRandom.nextDouble(<span class="number">0</span>, <span class="number">1</span>);</span><br><span class="line">      sortRateList.add(random);</span><br><span class="line">      Collections.sort(sortRateList);</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 返回该随机数在比例集合中的索引</span></span><br><span class="line">      <span class="keyword">return</span> sortRateList.indexOf(random);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="最大公约数"><a href="#最大公约数" class="headerlink" title="最大公约数"></a>最大公约数</h5><p>最大公约数与最小公倍数算法<code>com.lvshen.demo.arithmetic.maxCommon</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 欧几里得算法</span></span><br><span class="line"><span class="comment">    * 两个非零整数的最大公约数等于其中较小的那个数和两个数相除余数的最大公约数</span></span><br><span class="line"><span class="comment">    * <span class="doctag">@param</span> n</span></span><br><span class="line"><span class="comment">    * <span class="doctag">@param</span> m</span></span><br><span class="line"><span class="comment">    * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">int</span> <span class="title">getMaxCommonFromEuclid</span><span class="params">(<span class="keyword">int</span> m, <span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">    log.info(<span class="string">"testStart...."</span>);</span><br><span class="line">       <span class="comment">// m和n的最大公约数 = n 和 m%n的最大公约数</span></span><br><span class="line">   <span class="keyword">if</span> (n == <span class="number">0</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> m;</span><br><span class="line">   &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> getMaxCommonFromEuclid(n, m % n);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> n = <span class="number">6</span>;</span><br><span class="line">    <span class="keyword">int</span> m = <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> maxCommon = getMaxCommonFromEuclid(n, m);</span><br><span class="line">    System.out.println(<span class="string">"最大公约数："</span> + maxCommon);</span><br><span class="line">    System.out.println(<span class="string">"最小公倍数："</span> + n * m / maxCommon);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="队列解密"><a href="#队列解密" class="headerlink" title="队列解密"></a>队列解密</h5><p>队列使用<code>com.lvshen.demo.arithmetic.queue</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Description:算法 新学期开始了，小哈是小哼的新同桌（小哈是个小美女哦~），小哼向小哈询问 QQ 号， 小哈当然不会直接告诉小哼啦，原因嘛你懂的。所以小哈给了小哼一串加密过的数字，同</span></span><br><span class="line"><span class="comment"> * 时小哈也告诉了小哼解密规则。规则是这样的：首先将第 1 个数删除，紧接着将第 2 个数放到这串数的末尾，再将第 3个数删除并将第 4 个数再放到这串数的末尾，再将第 5</span></span><br><span class="line"><span class="comment"> * 个数删除……直到剩下最后一个数，将最后一个数也删除。按照刚才删除的顺序，把这些 删除的数连在一起就是小哈的 QQ 啦。现在你来帮帮小哼吧。小哈给小哼加密过的一串数 是“6 3 1 75 8 9 2 4”。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> </span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 采用队列方法</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		Queue&lt;Integer&gt; queue = <span class="keyword">new</span> LinkedList&lt;&gt;();</span><br><span class="line">        Integer[] q = &#123; <span class="number">6</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">7</span>, <span class="number">5</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">2</span>, <span class="number">4</span> &#125;;</span><br><span class="line">		List&lt;Integer&gt; list = Lists.newArrayList();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; q.length; i++) &#123;</span><br><span class="line">            queue.offer(q[i]);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">while</span> (queue.element() != <span class="keyword">null</span>) &#123;</span><br><span class="line">			list.add(queue.poll());</span><br><span class="line">			queue.offer(queue.poll());</span><br><span class="line">            System.out.println(queue);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(list.toString());</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<h5 id="搜索算法"><a href="#搜索算法" class="headerlink" title="搜索算法"></a>搜索算法</h5><p>搜索算法【广度优先与深度优先】<code>com.lvshen.demo.arithmetic.search</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">BFSSearch</span><span class="params">(String startPoint)</span> </span>&#123;</span><br><span class="line">    queue.add(startPoint);</span><br><span class="line">    status.put(startPoint,<span class="keyword">false</span>);</span><br><span class="line">    bfsLoop();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">DFSSearch</span><span class="params">(String startPoint)</span> </span>&#123;</span><br><span class="line">    stack.push(startPoint);</span><br><span class="line">    status.put(startPoint, <span class="keyword">true</span>);</span><br><span class="line">    dfsLoop();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//深度优先</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">dfsLoop</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (stack.isEmpty()) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    String stackTopPoint = stack.peek();</span><br><span class="line">    Map&lt;String, List&lt;String&gt;&gt; graphData = initGraphData();</span><br><span class="line">    List&lt;String&gt; neighborPoints = graphData.get(stackTopPoint);</span><br><span class="line">    neighborPoints.stream().forEach(x -&gt; &#123;</span><br><span class="line">        <span class="keyword">if</span> (status.getOrDefault(x,<span class="keyword">false</span>)) &#123;</span><br><span class="line">            stack.push(x);</span><br><span class="line">            status.put(x, <span class="keyword">true</span>);</span><br><span class="line">            dfsLoop();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    String popPoint = stack.pop();</span><br><span class="line">    System.out.println(popPoint);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//广度优先</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">bfsLoop</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    String currentQueueHeader = queue.poll();</span><br><span class="line">    status.put(currentQueueHeader, <span class="keyword">true</span>);</span><br><span class="line">    System.out.println(currentQueueHeader);</span><br><span class="line"></span><br><span class="line">    Map&lt;String, List&lt;String&gt;&gt; graphData = initGraphData();</span><br><span class="line">    List&lt;String&gt; neighborPoints = graphData.get(currentQueueHeader);</span><br><span class="line">    neighborPoints.stream().forEach(x -&gt; &#123;</span><br><span class="line">        <span class="keyword">if</span> (!status.getOrDefault(x, <span class="keyword">false</span>)) &#123;</span><br><span class="line">            <span class="keyword">if</span> (queue.contains(x)) &#123;</span><br><span class="line">                log.info(<span class="string">"the point is in queue!"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            queue.add(x);</span><br><span class="line">            status.put(x, <span class="keyword">false</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">if</span> (!queue.isEmpty()) &#123;</span><br><span class="line">        bfsLoop();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="长链转短链"><a href="#长链转短链" class="headerlink" title="长链转短链"></a>长链转短链</h5><p>Redis长链转短链算法<code>com.lvshen.demo.arithmetic.shorturl</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">getShortUrl</span><span class="params">(String longUrl, Decimal decimal)</span> </span>&#123;</span><br><span class="line">    String cache = redisTemplate.opsForValue().get(CACHE_KEY_PREFIX + longUrl);</span><br><span class="line">    <span class="keyword">if</span> (cache != <span class="keyword">null</span>) &#123;</span><br><span class="line">        log.info(<span class="string">"从缓存【&#123;&#125;】中获取到：&#123;&#125;"</span>,CACHE_KEY_PREFIX + longUrl,cache);</span><br><span class="line">        <span class="keyword">return</span> LOCALHOST + toOtherBaseString(Long.valueOf(cache), decimal.x);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    Long num = redisTemplate.opsForValue().increment(SHORT_URL_KEY);</span><br><span class="line">    redisTemplate.opsForValue().set(SHORT_LONG_PREFIX + num, longUrl);</span><br><span class="line"></span><br><span class="line">    redisTemplate.opsForValue().set(CACHE_KEY_PREFIX + longUrl, String.valueOf(num), CACHE_SECONDS, TimeUnit.SECONDS);</span><br><span class="line">    <span class="keyword">return</span> LOCALHOST + toOtherBaseString(num, decimal.x);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testShortUrl</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        String url = <span class="string">"www.google.com"</span>;</span><br><span class="line"></span><br><span class="line">        String shortUrl = shortUrlUtil.getShortUrl(url, ShortUrlUtil.Decimal.D64);</span><br><span class="line">        System.out.println(<span class="string">"短链："</span> + shortUrl);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h5 id="雪花算法"><a href="#雪花算法" class="headerlink" title="雪花算法"></a>雪花算法</h5><p>雪花算法<code>com.lvshen.demo.arithmetic.snowflake</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">synchronized</span> <span class="keyword">long</span> <span class="title">next</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">long</span> nowTimestamp = timeGen();</span><br><span class="line">    <span class="keyword">if</span> (nowTimestamp &lt; lastTimestamp) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> IllegalStateException(<span class="string">"系统时钟有误"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 当前时间戳跟上次的时间戳一样，毫秒内生成序列</span></span><br><span class="line">    <span class="keyword">if</span> (lastTimestamp == nowTimestamp) &#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/* 生成序列的掩码，127 (1111111=0x7f=127) */</span></span><br><span class="line">        <span class="keyword">long</span> sequenceMask = <span class="number">127L</span>;</span><br><span class="line">        sequence = (sequence + <span class="number">1</span>) &amp; sequenceMask;</span><br><span class="line">        <span class="comment">// 毫秒内序列溢出</span></span><br><span class="line">        <span class="keyword">if</span> (sequence == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="comment">// 阻塞到下一个毫秒,获得新的时间戳</span></span><br><span class="line">            nowTimestamp = timeGen();</span><br><span class="line">            <span class="keyword">while</span> (nowTimestamp &lt;= lastTimestamp) &#123;</span><br><span class="line">                nowTimestamp = timeGen();</span><br><span class="line">            &#125;</span><br><span class="line">            lastTimestamp = nowTimestamp;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        sequence = <span class="number">0</span>;</span><br><span class="line">        lastTimestamp = nowTimestamp;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//Date formatDate = DateTimeUtils.getFormatDate("2027-09-03");</span></span><br><span class="line">    <span class="comment">//nowTimestamp = formatDate.getTime();</span></span><br><span class="line">    <span class="comment">//同一服务器上面的不同应用可能生成同一id</span></span><br><span class="line">    <span class="keyword">long</span> id2Long = ((nowTimestamp - baseTimestamp) &lt;&lt; TIMESTAMP_LEFT_SHIFT) | workId | sequence;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> id2Long;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@org</span>.junit.Test</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span> <span class="keyword">throws</span> ParseException </span>&#123;</span><br><span class="line">        String id = <span class="keyword">new</span> SnowFlakeGenerator().nextId();</span><br><span class="line">        System.out.println(<span class="string">"id生成成功："</span> + id + <span class="string">",长度："</span> + id.length());</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h5 id="排序算法"><a href="#排序算法" class="headerlink" title="排序算法"></a>排序算法</h5><p>排序算法<code>com.lvshen.demo.arithmetic.sort</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">   <span class="comment">// 1.插入排序</span></span><br><span class="line">   <span class="comment">// printArrays(insertionSort(array));</span></span><br><span class="line">   <span class="comment">// 2.冒泡排序</span></span><br><span class="line">   <span class="comment">// printArrays(bubbleSort(array));</span></span><br><span class="line">   <span class="comment">// 3.选择排序</span></span><br><span class="line">   <span class="comment">// printArrays(selectionSort(array));</span></span><br><span class="line">   <span class="comment">// 4.希尔排序</span></span><br><span class="line">   <span class="comment">// printArrays(shellSort(array));</span></span><br><span class="line">   <span class="comment">// 5.快速排序</span></span><br><span class="line">   printArrays(quickSort(array,<span class="number">0</span>,<span class="number">4</span>));</span><br><span class="line">   <span class="comment">/*// 6.归并排序</span></span><br><span class="line"><span class="comment">   printArrays(mergeSort(array));</span></span><br><span class="line"><span class="comment">   // 7.计数排序</span></span><br><span class="line"><span class="comment">   printArrays(countingSort(array));</span></span><br><span class="line"><span class="comment">   // 8.堆排序</span></span><br><span class="line"><span class="comment">   printArrays(heapSort(array));*/</span></span><br><span class="line">   <span class="comment">// 9.桶排序</span></span><br><span class="line">   <span class="comment">// printLists(bucketSort(Ints.asList(array), 5));</span></span><br><span class="line">   <span class="comment">//10.基数排序</span></span><br><span class="line">   <span class="comment">// printArrays(radixSort(array));</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="回文数判断算法"><a href="#回文数判断算法" class="headerlink" title="回文数判断算法"></a>回文数判断算法</h5><p>栈使用<code>com.lvshen.demo.arithmetic.stack</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//回文数判断</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">boolean</span> <span class="title">palindromeTest</span><span class="params">(String param)</span> </span>&#123;</span><br><span class="line">   <span class="keyword">if</span> (StringUtils.isBlank(param)) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">   &#125;</span><br><span class="line">   Stack&lt;String&gt; stack = <span class="keyword">new</span> Stack&lt;&gt;();</span><br><span class="line">   <span class="keyword">char</span>[] chars = param.toCharArray();</span><br><span class="line">   <span class="keyword">int</span> mid = chars.length / <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; mid; i++) &#123;</span><br><span class="line">      stack.push(String.valueOf(chars[i]));</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">for</span> (<span class="keyword">int</span> i = mid + <span class="number">1</span>; i &lt;= chars.length - <span class="number">1</span>; i++) &#123;</span><br><span class="line">      <span class="comment">// 栈首获取</span></span><br><span class="line">      <span class="keyword">if</span> (!stack.peek().equals(String.valueOf(chars[i]))) &#123;</span><br><span class="line">         <span class="keyword">break</span>;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 出栈</span></span><br><span class="line">      stack.pop();</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="keyword">if</span> (stack.size() == <span class="number">0</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">   &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		String param = <span class="string">"席主席"</span>;</span><br><span class="line">		System.out.println(palindromeTest(param));</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<h5 id="订单编号生成算法"><a href="#订单编号生成算法" class="headerlink" title="订单编号生成算法"></a>订单编号生成算法</h5><p>订单编号生成算法<code>com.lvshen.demo.arithmetic.tradno</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> UnknownHostException </span>&#123;</span><br><span class="line">   <span class="comment">// 1.两位随机数+13位时间戳</span></span><br><span class="line">   <span class="keyword">int</span> r1 = (<span class="keyword">int</span>) (Math.random() * (<span class="number">10</span>));<span class="comment">// 产生2个0-9的随机数</span></span><br><span class="line">   <span class="keyword">int</span> r2 = (<span class="keyword">int</span>) (Math.random() * (<span class="number">10</span>));</span><br><span class="line">   <span class="keyword">long</span> now = System.currentTimeMillis();<span class="comment">// 一个13位的时间戳</span></span><br><span class="line">   String paymentID = String.valueOf(r1) + String.valueOf(r2) + String.valueOf(now);<span class="comment">// 订单ID</span></span><br><span class="line">   System.out.println(paymentID);</span><br><span class="line"></span><br><span class="line">   <span class="comment">// 2.年月日+用户主键.hashCode()+ip</span></span><br><span class="line">   InetAddress addrs = InetAddress.getLocalHost();</span><br><span class="line">   System.out.println(<span class="string">"Local HostAddress: "</span> + addrs.getHostAddress());</span><br><span class="line">   String hostName = addrs.getHostName();</span><br><span class="line">   System.out.println(<span class="string">"Local host name: "</span> + hostName);</span><br><span class="line">   String userId = <span class="string">"admin"</span>;</span><br><span class="line"></span><br><span class="line">   <span class="comment">// 当前时间</span></span><br><span class="line">   Date date = <span class="keyword">new</span> Date();</span><br><span class="line">   <span class="comment">// 转换格式</span></span><br><span class="line">   SimpleDateFormat simpleDateFormat = <span class="keyword">new</span> SimpleDateFormat(<span class="string">"yyyyMMdd"</span>);</span><br><span class="line">   <span class="comment">// 格式化</span></span><br><span class="line">   String time = simpleDateFormat.format(date);</span><br><span class="line">   <span class="comment">// ip地址</span></span><br><span class="line">   String ip = addrs.getHostAddress();</span><br><span class="line">   ip = ip.replace(<span class="string">"."</span>, <span class="string">""</span>);</span><br><span class="line"></span><br><span class="line">   System.currentTimeMillis(); <span class="comment">// 获取毫秒</span></span><br><span class="line"></span><br><span class="line">   String id = time + ip + userId.hashCode() + System.currentTimeMillis();</span><br><span class="line">   System.out.println(<span class="string">"生成唯一订单号"</span> + id);</span><br><span class="line">   System.out.println(<span class="string">"订单号"</span> + ip + System.currentTimeMillis());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目三：幂等注解"><a href="#项目三：幂等注解" class="headerlink" title="项目三：幂等注解"></a>项目三：幂等注解</h4><p>幂等注解<code>com.lvshen.demo.autoidempotent</code>;</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@AutoIdempotent</span></span><br><span class="line"><span class="meta">@PostMapping</span>(<span class="string">"/test/Idempotence"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> ServerResponse <span class="title">testIdempotence</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> testService.testIdempotence();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 检验token</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> request</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">checkToken</span><span class="params">(HttpServletRequest request)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"></span><br><span class="line">        String token = request.getHeader(HEADER_KEY);</span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isBlank(token)) &#123;<span class="comment">// header中不存在token</span></span><br><span class="line">            token = request.getParameter(HEADER_KEY);</span><br><span class="line">            <span class="keyword">if</span> (StringUtils.isBlank(token)) &#123;<span class="comment">// parameter中也不存在token</span></span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> ServiceException(ResponseCode.ILLEGAL_ARGUMENT.getMsg());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (!redisTemplate.hasKey(token)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> ServiceException(ResponseCode.REPETITIVE_OPERATION.getMsg());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">boolean</span> remove = redisTemplate.delete(token);</span><br><span class="line">        <span class="keyword">if</span> (!remove) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> ServiceException(ResponseCode.REPETITIVE_OPERATION.getMsg());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">preHandle</span><span class="params">(HttpServletRequest request, HttpServletResponse response, Object handler)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (!(handler <span class="keyword">instanceof</span> HandlerMethod)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        HandlerMethod handlerMethod = (HandlerMethod) handler;</span><br><span class="line">        Method method = handlerMethod.getMethod();</span><br><span class="line">        <span class="comment">//被ApiIdempotment标记的扫描</span></span><br><span class="line">        AutoIdempotent methodAnnotation = method.getAnnotation(AutoIdempotent<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">        <span class="keyword">if</span> (methodAnnotation != <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> tokenService.checkToken(request);<span class="comment">// 幂等性校验, 校验通过则放行, 校验失败则抛出异常, 并通过统一异常处理返回友好提示</span></span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception ex) &#123;</span><br><span class="line">                ResultVo failedResult = ResultVo.getFailedResult(<span class="number">101</span>, ((ServiceException) ex).getMsg());</span><br><span class="line">                writeReturnJson(response, JSONObject.toJSONString(failedResult));</span><br><span class="line">                <span class="keyword">throw</span> ex;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//必须返回true,否则会被拦截一切请求</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目四：Java基础"><a href="#项目四：Java基础" class="headerlink" title="项目四：Java基础"></a>项目四：Java基础</h4><h5 id="BigDecimal"><a href="#BigDecimal" class="headerlink" title="BigDecimal"></a>BigDecimal</h5><p>BigDecimal使用<code>com.lvshen.demo.bigdecimal</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span></span>&#123;</span><br><span class="line">    BigDecimal bigDecimal = <span class="keyword">new</span> BigDecimal(<span class="number">0.1</span>);</span><br><span class="line">    System.out.println(bigDecimal);</span><br><span class="line"></span><br><span class="line">    BigDecimal bigDecimal1 = <span class="keyword">new</span> BigDecimal(<span class="number">10</span>);</span><br><span class="line">    System.out.println(bigDecimal1);</span><br><span class="line"></span><br><span class="line">    BigDecimal bigDecimal2 = <span class="keyword">new</span> BigDecimal(<span class="string">"0.1"</span>);</span><br><span class="line">    System.out.println(bigDecimal2);</span><br><span class="line"></span><br><span class="line">    BigDecimal bigDecimal3 = BigDecimal.valueOf(<span class="number">0.1</span>);</span><br><span class="line">    System.out.println(bigDecimal3);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="CAS"><a href="#CAS" class="headerlink" title="CAS"></a>CAS</h5><p>CAS使用：<code>com.lvshen.demo.cas</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyAtomicInteger</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">volatile</span> <span class="keyword">int</span> value;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">long</span> offset;<span class="comment">//偏移地址</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> Unsafe unsafe;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");</span><br><span class="line">            theUnsafeField.setAccessible(<span class="keyword">true</span>);</span><br><span class="line">            unsafe = (Unsafe) theUnsafeField.get(<span class="keyword">null</span>);</span><br><span class="line">            Field field = MyAtomicInteger.class.getDeclaredField("value");</span><br><span class="line">            offset = unsafe.objectFieldOffset(field);<span class="comment">//获得偏移地址</span></span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">increment</span><span class="params">(<span class="keyword">int</span> num)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> tempValue;</span><br><span class="line">        <span class="keyword">do</span> &#123;</span><br><span class="line">            tempValue = unsafe.getIntVolatile(<span class="keyword">this</span>, offset);<span class="comment">//拿到值</span></span><br><span class="line">        &#125; <span class="keyword">while</span> (!unsafe.compareAndSwapInt(<span class="keyword">this</span>, offset, tempValue, value + num));<span class="comment">//CAS自旋</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">get</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> value;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="volatile"><a href="#volatile" class="headerlink" title="volatile"></a>volatile</h5><p>volatile使用：<code>com.lvshen.demo.concurrent</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">VolatileVisibilityTest</span> </span>&#123;</span><br><span class="line">    <span class="comment">//private static volatile boolean initFlag = false;</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">boolean</span> initFlag = <span class="keyword">false</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        <span class="keyword">new</span> Thread(() -&gt; &#123;</span><br><span class="line">            System.out.println(<span class="string">"waiting data"</span>);</span><br><span class="line">            <span class="keyword">while</span> (!initFlag) &#123;</span><br><span class="line">                <span class="comment">//System.out.println();</span></span><br><span class="line">                <span class="comment">//里面添加锁共享变量就不会缓存</span></span><br><span class="line">                <span class="keyword">synchronized</span> (VolatileVisibilityTest<span class="class">.<span class="keyword">class</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(<span class="string">"========success!!!"</span>);</span><br><span class="line">        &#125;).start();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        Thread.sleep(<span class="number">2000</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">new</span> Thread(() -&gt; prepareData()).start();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">prepareData</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"开始修改initFlag..."</span>);</span><br><span class="line">        initFlag = <span class="keyword">true</span>;</span><br><span class="line">        System.out.println(<span class="string">"initFlag修改成功..."</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="Date"><a href="#Date" class="headerlink" title="Date"></a>Date</h5><p>日期处理：<code>com.lvshen.demo.date</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> Date <span class="title">getDate</span><span class="params">(String dateStr)</span> <span class="keyword">throws</span> ParseException </span>&#123;</span><br><span class="line">    dateStr = dateStr.replace(<span class="string">"Z"</span>, <span class="string">" UTC"</span>);<span class="comment">//是空格+UTC</span></span><br><span class="line">    SimpleDateFormat format = <span class="keyword">new</span> SimpleDateFormat(<span class="string">"yyyy-MM-dd'T'HH:mm:ss.SSS Z"</span>);<span class="comment">//格式化的表达式</span></span><br><span class="line">    <span class="keyword">return</span> format.parse(dateStr);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>instanceof使用：<code>com.lvshen.demo.extendsTest</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">testType</span><span class="params">(Object o)</span> </span>&#123;</span><br><span class="line">   <span class="keyword">if</span> (o <span class="keyword">instanceof</span> Person) &#123;</span><br><span class="line">      System.out.println(<span class="string">"this is person"</span>);</span><br><span class="line">   &#125; <span class="keyword">else</span> <span class="keyword">if</span> (o <span class="keyword">instanceof</span> Student) &#123;</span><br><span class="line">      System.out.println(<span class="string">"this is student"</span>);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>Integer使用：<code>com.lvshen.demo.integer</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">IntegerTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">      <span class="comment">// -128--127之间</span></span><br><span class="line"></span><br><span class="line">      Integer i1 = <span class="number">100</span>;</span><br><span class="line">      Integer i2 = <span class="number">100</span>;</span><br><span class="line">      <span class="keyword">if</span> (i1 == i2) &#123;</span><br><span class="line">         System.out.println(<span class="string">"i1 == i2"</span>);</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">         System.out.println(<span class="string">"i1 != i2 "</span>);</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span> (i1 &lt; i2) &#123;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 大于127</span></span><br><span class="line">      Integer i3 = <span class="number">200</span>;</span><br><span class="line">      Integer i4 = <span class="number">200</span>;</span><br><span class="line">      <span class="comment">/*if (i3 == i4) &#123;</span></span><br><span class="line"><span class="comment">         System.out.println("i3 == i4");</span></span><br><span class="line"><span class="comment">      &#125; else &#123;</span></span><br><span class="line"><span class="comment">         System.out.println("i3 != i4 ");   (√)&#125;*/</span></span><br><span class="line"></span><br><span class="line">      <span class="comment">/*if (i3.compareTo(i4) == 0) &#123;</span></span><br><span class="line"><span class="comment">            System.out.println("i3 == i4");    (√)</span></span><br><span class="line"><span class="comment">      &#125; else &#123;</span></span><br><span class="line"><span class="comment">            System.out.println("i3 != i4 ");</span></span><br><span class="line"><span class="comment">        &#125;*/</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (i3.intValue() == i4.intValue()) &#123;</span><br><span class="line">            System.out.println(<span class="string">"i3 == i4"</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">"i3 != i4 "</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="meta">@Test</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testInteger</span><span class="params">()</span> </span>&#123;</span><br><span class="line">      String str = <span class="string">"12"</span>;</span><br><span class="line">      <span class="keyword">int</span> parseInt = Integer.parseInt(str);</span><br><span class="line">      Integer valueOf = Integer.valueOf(str);</span><br><span class="line">      System.out.println(<span class="string">"parseInt"</span> + parseInt);</span><br><span class="line">      System.out.println(<span class="string">"valueOf"</span> + valueOf);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>地址传递与值传递：<code>com.lvshen.demo.javafoundation</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 基本数据类型：值传递</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span> </span>&#123;</span><br><span class="line">   <span class="keyword">int</span> x = <span class="number">10</span>;</span><br><span class="line">   <span class="keyword">int</span> y = <span class="number">20</span>;</span><br><span class="line">   swap(x, y);</span><br><span class="line"></span><br><span class="line">   System.out.println(<span class="string">"x[2] = "</span> + x);</span><br><span class="line">   System.out.println(<span class="string">"y[2] = "</span> + y);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 引用数据类型：地址传递（数组）</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">   <span class="keyword">int</span>[] a = &#123; <span class="number">10</span>, <span class="number">20</span> &#125;;</span><br><span class="line">   System.out.println(<span class="string">"a[0] :"</span> + a[<span class="number">0</span>] + <span class="string">", a[1] : "</span> + a[<span class="number">1</span>]);</span><br><span class="line">   swap(a, <span class="number">0</span>, <span class="number">1</span>);</span><br><span class="line">   System.out.println(<span class="string">"a[0] :"</span> + a[<span class="number">0</span>] + <span class="string">", a[1] : "</span> + a[<span class="number">1</span>]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>SecureRandom使用：<code>com.lvshen.demo.securerandom</code>；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">       <span class="keyword">byte</span>[] bytes = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">128</span>];</span><br><span class="line">       SecureRandom secureRandom = <span class="keyword">new</span> SecureRandom();</span><br><span class="line">       secureRandom.setSeed(System.currentTimeMillis());</span><br><span class="line">       secureRandom.nextBytes(bytes);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<p>StringBuilder使用：<code>com.lvshen.demo.stringbuildertest</code>；</p>
<h4 id="项目五：设计模式com-lvshen-demo-design"><a href="#项目五：设计模式com-lvshen-demo-design" class="headerlink" title="项目五：设计模式com.lvshen.demo.design"></a>项目五：设计模式<code>com.lvshen.demo.design</code></h4><p>抽象工厂，适配器，桥接，建造者，组合，装饰者，外观，工厂，原型，代理，单列….</p>
<h4 id="项目六：Zookeeper分布式锁com-lvshen-demo-distributelock"><a href="#项目六：Zookeeper分布式锁com-lvshen-demo-distributelock" class="headerlink" title="项目六：Zookeeper分布式锁com.lvshen.demo.distributelock"></a>项目六：Zookeeper分布式锁<code>com.lvshen.demo.distributelock</code></h4><blockquote>
<p>见博客文章：<a href="https://lvshen9.gitee.io/2020/04/19/1/">手写Zookeeper分布式锁</a></p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">lock</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!tryLock()) &#123;</span><br><span class="line">        <span class="comment">//没获得锁，阻塞自己</span></span><br><span class="line">        log.info(<span class="string">"线程&#123;&#125;没有获得锁，开始阻塞。"</span>,Thread.currentThread().getName());</span><br><span class="line">        waitForLock();</span><br><span class="line">        <span class="comment">//再次尝试</span></span><br><span class="line">        lock();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">tryLock</span><span class="params">()</span> </span>&#123;  <span class="comment">//不会阻塞</span></span><br><span class="line">        <span class="comment">//创建节点</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">this</span>.currentPath == <span class="keyword">null</span>) &#123;</span><br><span class="line">            currentPath = <span class="keyword">this</span>.client.createEphemeralSequential(lockPath + <span class="string">"/"</span>, <span class="string">"lock"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        List&lt;String&gt; children = <span class="keyword">this</span>.client.getChildren(lockPath);</span><br><span class="line">        Collections.sort(children);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (currentPath.equals(lockPath + <span class="string">"/"</span> + children.get(<span class="number">0</span>))) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">int</span> currentIndex = children.indexOf(currentPath.substring(lockPath.length() + <span class="number">1</span>));</span><br><span class="line">            beforePath = lockPath + <span class="string">"/"</span> + children.get(currentIndex - <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        log.info(<span class="string">"锁节点创建成功：&#123;&#125;"</span>, lockPath);</span><br><span class="line">        System.out.println(String.format(<span class="string">"锁节点创建成功：&#123;%s&#125;"</span>, lockPath));</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">waitForLock</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        CountDownLatch count = <span class="keyword">new</span> CountDownLatch(<span class="number">1</span>);</span><br><span class="line">        IZkDataListener listener = <span class="keyword">new</span> IZkDataListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">handleDataChange</span><span class="params">(String s, Object o)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">handleDataDeleted</span><span class="params">(String s)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">                log.info(<span class="string">"收到节点&#123;&#125;被删除了"</span>,s);</span><br><span class="line">                System.out.println(String.format(<span class="string">"收到节点[%s]被删除了"</span>,s));</span><br><span class="line">                count.countDown();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        client.subscribeDataChanges(<span class="keyword">this</span>.beforePath, listener);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//自己阻塞自己</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">this</span>.client.exists(<span class="keyword">this</span>.beforePath)) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                count.await();</span><br><span class="line">            &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//取消注册</span></span><br><span class="line">        client.unsubscribeDataChanges(<span class="keyword">this</span>.beforePath, listener);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目七：excel导出com-lvshen-demo-export"><a href="#项目七：excel导出com-lvshen-demo-export" class="headerlink" title="项目七：excel导出com.lvshen.demo.export"></a>项目七：excel导出<code>com.lvshen.demo.export</code></h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">exportExcel</span><span class="params">(String exportPath)</span> </span>&#123;</span><br><span class="line">   XSSFWorkbook workbook = <span class="keyword">new</span> XSSFWorkbook();</span><br><span class="line">   XSSFSheet sheet = workbook.createSheet(<span class="string">"测试"</span>);</span><br><span class="line">   <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">9</span>; i++) &#123;</span><br><span class="line">      sheet.setColumnWidth(i, <span class="number">4300</span>);</span><br><span class="line">   &#125;</span><br><span class="line">   setTitleStyle(sheet, workbook);</span><br><span class="line"></span><br><span class="line">   setExcelFooterName(<span class="string">"test"</span>, <span class="number">0</span>, workbook);</span><br><span class="line">   exportOutPutExcel(exportPath, workbook);</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目八：Guava使用com-lvshen-demo-guava-study"><a href="#项目八：Guava使用com-lvshen-demo-guava-study" class="headerlink" title="项目八：Guava使用com.lvshen.demo.guava.study"></a>项目八：Guava使用<code>com.lvshen.demo.guava.study</code></h4><p><code>Optional</code>，<code>Collections</code>，<code>String</code>，<code>Primitives</code></p>
<h4 id="项目九：kafka使用com-lvshen-demo-kafka"><a href="#项目九：kafka使用com-lvshen-demo-kafka" class="headerlink" title="项目九：kafka使用com.lvshen.demo.kafka"></a>项目九：kafka使用<code>com.lvshen.demo.kafka</code></h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 基本数据类型：值传递</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span> </span>&#123;</span><br><span class="line">   <span class="keyword">int</span> x = <span class="number">10</span>;</span><br><span class="line">   <span class="keyword">int</span> y = <span class="number">20</span>;</span><br><span class="line">   swap(x, y);</span><br><span class="line"></span><br><span class="line">   System.out.println(<span class="string">"x[2] = "</span> + x);</span><br><span class="line">   System.out.println(<span class="string">"y[2] = "</span> + y);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 引用数据类型：地址传递（数组）</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">   <span class="keyword">int</span>[] a = &#123; <span class="number">10</span>, <span class="number">20</span> &#125;;</span><br><span class="line">   System.out.println(<span class="string">"a[0] :"</span> + a[<span class="number">0</span>] + <span class="string">", a[1] : "</span> + a[<span class="number">1</span>]);</span><br><span class="line">   swap(a, <span class="number">0</span>, <span class="number">1</span>);</span><br><span class="line">   System.out.println(<span class="string">"a[0] :"</span> + a[<span class="number">0</span>] + <span class="string">", a[1] : "</span> + a[<span class="number">1</span>]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目十：spring-websocket使用com-lvshen-demo-kafka；"><a href="#项目十：spring-websocket使用com-lvshen-demo-kafka；" class="headerlink" title="项目十：spring-websocket使用com.lvshen.demo.kafka；"></a>项目十：spring-websocket使用<code>com.lvshen.demo.kafka</code>；</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="meta">@Slf</span>4j</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">KafkaConsumer</span> </span>&#123;</span><br><span class="line">    <span class="meta">@KafkaListener</span>(topics = <span class="string">"$&#123;spring.kafka.topic&#125;"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">listen</span><span class="params">(ConsumerRecord&lt;?, ?&gt; record)</span> </span>&#123;</span><br><span class="line">        log.info(<span class="string">"topic=&#123;&#125;, offset=&#123;&#125;, message=&#123;&#125;"</span>, record.topic(), record.offset(), record.value());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="meta">@Slf</span>4j</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">KafkaProducer</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> KafkaTemplate kafkaTemplate;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"$&#123;spring.kafka.topic&#125;"</span>)</span><br><span class="line">    <span class="keyword">private</span> String topic;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 发送kafka消息</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> jsonString</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">send</span><span class="params">(String jsonString)</span> </span>&#123;</span><br><span class="line">        ListenableFuture future = kafkaTemplate.send(topic, jsonString);</span><br><span class="line">        future.addCallback(o -&gt; log.info(<span class="string">"kafka消息发送成功："</span> + jsonString), throwable -&gt; log.error(<span class="string">"kafka消息发送失败："</span> + jsonString));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testDemo</span><span class="params">()</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">		log.info(<span class="string">"start send"</span>);</span><br><span class="line">		kafkaProducer.send(<span class="string">"中文测试222"</span>);</span><br><span class="line">		log.info(<span class="string">"end send"</span>);</span><br><span class="line">		<span class="comment">// 休眠10秒，为了使监听器有足够的时间监听到topic的数据</span></span><br><span class="line">		Thread.sleep(<span class="number">10</span>);</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目十一：手写Lock-com-lvshen-demo-lock；"><a href="#项目十一：手写Lock-com-lvshen-demo-lock；" class="headerlink" title="项目十一：手写Lock  com.lvshen.demo.lock；"></a>项目十一：手写Lock  <code>com.lvshen.demo.lock</code>；</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">lock</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!tryLock()) &#123;</span><br><span class="line">        <span class="comment">//抢不到锁的线程放入阻塞队列中排队</span></span><br><span class="line">        waiters.add(Thread.currentThread());</span><br><span class="line">        <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (tryLock()) &#123;</span><br><span class="line">                waiters.poll();<span class="comment">//抢到锁，自己移出队列</span></span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">//阻塞</span></span><br><span class="line">                LockSupport.park();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">tryLock</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (state.get() == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (state.compareAndSet(<span class="number">0</span>, <span class="number">1</span>)) &#123;</span><br><span class="line">                ownerThread = Thread.currentThread();</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//可重入锁设计</span></span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (ownerThread == Thread.currentThread()) &#123;</span><br><span class="line">            state.set(state.get() + <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">unlock</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (ownerThread != Thread.currentThread()) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"非法调用，当前线程并没有持有该锁"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (state.decrementAndGet() == <span class="number">0</span>) &#123;</span><br><span class="line">            ownerThread = <span class="keyword">null</span>;</span><br><span class="line">            <span class="comment">//通知其他等待抢锁的线程</span></span><br><span class="line">            Thread waiterThread = waiters.peek();</span><br><span class="line">            <span class="keyword">if</span> (waiterThread != <span class="keyword">null</span>) &#123;</span><br><span class="line">                LockSupport.unpark(waiterThread);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目十二：mybatis开发-com-lvshen-demo-member；"><a href="#项目十二：mybatis开发-com-lvshen-demo-member；" class="headerlink" title="项目十二：mybatis开发 com.lvshen.demo.member；"></a>项目十二：mybatis开发 <code>com.lvshen.demo.member</code>；</h4><h5 id="entity"><a href="#entity" class="headerlink" title="entity"></a>entity</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@AllArgsConstructor</span></span><br><span class="line"><span class="meta">@NoArgsConstructor</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Member</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String id;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> Integer code;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Transient</span></span><br><span class="line">    <span class="meta">@NeedSetValue</span>(beanClass = BeanClassTest<span class="class">.<span class="keyword">class</span>, <span class="title">param</span> </span>= <span class="string">"code"</span>, method = <span class="string">"getMemberByCode"</span>, targetFiled = <span class="string">"name"</span>)</span><br><span class="line">    <span class="keyword">private</span> String annotationParam;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="mapper"><a href="#mapper" class="headerlink" title="mapper"></a>mapper</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Mapper</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">MemberMapper</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function">List&lt;Member&gt; <span class="title">listByName</span><span class="params">(String name)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="service"><a href="#service" class="headerlink" title="service"></a>service</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MemberService</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> MemberMapper memberMapper;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Cacheable</span>(value = <span class="string">"member"</span>,key = <span class="string">"#name"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;Member&gt; <span class="title">listByName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">       <span class="keyword">return</span> memberMapper.listByName(name);</span><br><span class="line">    &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<h5 id="controller"><a href="#controller" class="headerlink" title="controller"></a>controller</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(value = <span class="string">"/member"</span>, method = &#123;RequestMethod.GET, RequestMethod.POST&#125;)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MemberController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> MemberService memberService;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@RequestMapping</span>(<span class="string">"/listByName"</span>)</span><br><span class="line">    <span class="meta">@ResponseBody</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;Member&gt; <span class="title">listByName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> memberService.listByName(name);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="Mapper-xml"><a href="#Mapper-xml" class="headerlink" title="Mapper.xml"></a>Mapper.xml</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.lvshen.demo.member.mapper.MemberMapper"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"result"</span> <span class="attr">type</span>=<span class="string">"com.lvshen.demo.member.entity.Member"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"name"</span> <span class="attr">column</span>=<span class="string">"name"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"id"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"code"</span> <span class="attr">column</span>=<span class="string">"code"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"listByName"</span> <span class="attr">resultMap</span>=<span class="string">"result"</span>&gt;</span></span><br><span class="line">      SELECT * FROM member where name=#&#123;name&#125;</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">    </span><br><span class="line"><span class="tag">&lt;/<span class="name">mapper</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="项目十三：redis开发-com-lvshen-demo-redis；"><a href="#项目十三：redis开发-com-lvshen-demo-redis；" class="headerlink" title="项目十三：redis开发 com.lvshen.demo.redis；"></a>项目十三：redis开发 <code>com.lvshen.demo.redis</code>；</h4><p>缓存注解，延迟队列，分布式锁，限流注解，可重入锁，分布式session，发布订阅</p>
<h5 id="分布式缓存"><a href="#分布式缓存" class="headerlink" title="分布式缓存"></a>分布式缓存</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="meta">@Slf</span>4j</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CacheAspect</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> RedisTemplate redisTemplate;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Pointcut</span>(<span class="string">"@annotation(com.lvshen.demo.redis.cache.CustomizeCache)"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">cachePointcut</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Around</span>(<span class="string">"cachePointcut()"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">doCache</span><span class="params">(ProceedingJoinPoint point)</span> </span>&#123;</span><br><span class="line">        Object value = <span class="keyword">null</span>;</span><br><span class="line">        Semaphore semaphore = <span class="keyword">null</span>;</span><br><span class="line">        MethodSignature signature = (MethodSignature) point.getSignature();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//获取方法上注解的类容</span></span><br><span class="line">            Method method = point.getTarget().getClass().getMethod(signature.getName(), signature.getMethod().getParameterTypes());</span><br><span class="line">            CustomizeCache annotation = method.getAnnotation(CustomizeCache<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">            String keyEl = annotation.key();</span><br><span class="line">            String prefix = annotation.value();</span><br><span class="line">            <span class="keyword">long</span> expireTimes = annotation.expireTimes();</span><br><span class="line">            <span class="keyword">int</span> semaphoreCount = annotation.semaphoreCount();</span><br><span class="line"></span><br><span class="line">            <span class="comment">//解析SpringEL表达式</span></span><br><span class="line">            SpelExpressionParser parser = <span class="keyword">new</span> SpelExpressionParser();</span><br><span class="line">            Expression expression = parser.parseExpression(keyEl);</span><br><span class="line">            StandardEvaluationContext context = <span class="keyword">new</span> StandardEvaluationContext();</span><br><span class="line"></span><br><span class="line">            <span class="comment">//添加参数</span></span><br><span class="line">            Object[] args = point.getArgs();</span><br><span class="line">            DefaultParameterNameDiscoverer discoverer = <span class="keyword">new</span> DefaultParameterNameDiscoverer();</span><br><span class="line">            String[] parameterNames = discoverer.getParameterNames(method);</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; parameterNames.length; i++) &#123;</span><br><span class="line">                context.setVariable(parameterNames[i], args[i].toString());</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//解析</span></span><br><span class="line">            String key = prefix + <span class="string">"::"</span> + expression.getValue(context).toString();</span><br><span class="line"></span><br><span class="line">            <span class="comment">//判断缓存中是否存在</span></span><br><span class="line">            value = redisTemplate.opsForValue().get(key);</span><br><span class="line">            <span class="keyword">if</span> (value != <span class="keyword">null</span>) &#123;</span><br><span class="line">                log.info(<span class="string">"从缓存中读取到值：&#123;&#125;"</span>, value);</span><br><span class="line">                <span class="keyword">return</span> value;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//自定义组件，如：限流，降级。。。</span></span><br><span class="line">            <span class="comment">//创建限流令牌</span></span><br><span class="line">            semaphore = <span class="keyword">new</span> Semaphore(semaphoreCount);</span><br><span class="line">            <span class="keyword">boolean</span> tryAcquire = semaphore.tryAcquire(<span class="number">3000L</span>, TimeUnit.MILLISECONDS);</span><br><span class="line">            <span class="keyword">if</span> (!tryAcquire) &#123;</span><br><span class="line">                <span class="comment">//log.info("当前线程【&#123;&#125;】获取令牌失败,等带其他线程释放令牌", Thread.currentThread().getName());</span></span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(String.format(<span class="string">"当前线程【%s】获取令牌失败,等带其他线程释放令牌"</span>, Thread.currentThread().getName()));</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//缓存不存在则执行方法</span></span><br><span class="line">            value = point.proceed();</span><br><span class="line"></span><br><span class="line">            <span class="comment">//同步value到缓存</span></span><br><span class="line">            redisTemplate.opsForValue().set(key, value, expireTimes, TimeUnit.SECONDS);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">catch</span> (Throwable t) &#123;</span><br><span class="line">            t.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (semaphore == <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> value;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                semaphore.release();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> value;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="分布式session"><a href="#分布式session" class="headerlink" title="分布式session"></a>分布式session</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping</span>(value = <span class="string">"/testSession"</span>,method = RequestMethod.GET)</span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">testSession</span><span class="params">(HttpSession session, Model model)</span> </span>&#123;</span><br><span class="line">    List&lt;Member&gt; members = memberService.listMember();</span><br><span class="line">    System.out.println(<span class="string">"sessionId------&gt;"</span> + session.getId());</span><br><span class="line">    model.addAttribute(<span class="string">"member"</span>, JSON.toJSONString(members));</span><br><span class="line">    session.setAttribute(<span class="string">"member"</span>,JSON.toJSONString(members));</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"hello world"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="发布与订阅"><a href="#发布与订阅" class="headerlink" title="发布与订阅"></a>发布与订阅</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Publisher</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> RedisTemplate redisTemplate;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">pushMessage</span><span class="params">(String topic, RedisMessage message)</span> </span>&#123;</span><br><span class="line">        redisTemplate.convertAndSend(topic,message);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@Slf</span>4j</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserReceiver</span> <span class="keyword">extends</span> <span class="title">AbstractReceiver</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">receiveMessage</span><span class="params">(Object message)</span> </span>&#123;</span><br><span class="line">        log.info(<span class="string">"接收到用户消息：&#123;&#125;"</span>, JSON.toJSONString(message));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//redis发布订阅 pub/sub</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">pushMessage</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        UserMessage userMessage = <span class="keyword">new</span> UserMessage();</span><br><span class="line">        userMessage.setMsgId(UUID.randomUUID().toString().replace(<span class="string">"-"</span>, <span class="string">""</span>));</span><br><span class="line">        userMessage.setUserId(<span class="string">"1"</span>);</span><br><span class="line">        userMessage.setUsername(<span class="string">"admin"</span>);</span><br><span class="line">        userMessage.setUsername(<span class="string">"root"</span>);</span><br><span class="line">        userMessage.setCreateStamp(System.currentTimeMillis());</span><br><span class="line">        publisher.pushMessage(<span class="string">"user"</span>, userMessage);</span><br><span class="line">        GoodsMessage goodsMessage = <span class="keyword">new</span> GoodsMessage();</span><br><span class="line">        goodsMessage.setMsgId(UUID.randomUUID().toString().replace(<span class="string">"-"</span>, <span class="string">""</span>));</span><br><span class="line">        goodsMessage.setGoodsType(<span class="string">"苹果"</span>);</span><br><span class="line">        goodsMessage.setNumber(<span class="string">"十箱"</span>);</span><br><span class="line">        goodsMessage.setCreateStamp(System.currentTimeMillis());</span><br><span class="line">        publisher.pushMessage(<span class="string">"goods"</span>, goodsMessage);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h5 id="延迟队列与限流实现"><a href="#延迟队列与限流实现" class="headerlink" title="延迟队列与限流实现"></a>延迟队列与限流实现</h5><blockquote>
<p><a href="https://lvshen9.gitee.io/2020/04/23/1/">Redis实现延迟队列</a></p>
<p><a href="https://lvshen9.gitee.io/2020/04/25/1/">用Redis实现接口限流</a></p>
</blockquote>
<h5 id="可重入锁"><a href="#可重入锁" class="headerlink" title="可重入锁"></a>可重入锁</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RedisWithReentrantLock</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> RedisTemplate redisTemplate;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String REDIS_VALUE = <span class="string">"r_lock"</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> ThreadLocal&lt;Map&lt;String, Integer&gt;&gt; lockers = <span class="keyword">new</span> ThreadLocal&lt;&gt;();</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">boolean</span> <span class="title">_lock</span><span class="params">(String key)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> redisTemplate.opsForValue().setIfAbsent(key, REDIS_VALUE, <span class="number">20</span>, TimeUnit.SECONDS);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">_unlock</span><span class="params">(String key)</span> </span>&#123;</span><br><span class="line">        redisTemplate.delete(key);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> Map&lt;String, Integer&gt; <span class="title">currentLockers</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Map&lt;String, Integer&gt; refs = lockers.get();</span><br><span class="line">        <span class="keyword">if</span> (refs != <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> refs;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        lockers.set(<span class="keyword">new</span> HashMap&lt;&gt;());</span><br><span class="line">        <span class="keyword">return</span> lockers.get();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">lock</span><span class="params">(String key)</span> </span>&#123;</span><br><span class="line">        Map&lt;String, Integer&gt; refs = currentLockers();</span><br><span class="line">        Integer refCnt = refs.get(key);</span><br><span class="line">        <span class="keyword">if</span> (refCnt != <span class="keyword">null</span>) &#123;</span><br><span class="line">            refs.put(key, refCnt + <span class="number">1</span>);</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">boolean</span> ok = <span class="keyword">this</span>._lock(key);</span><br><span class="line">        <span class="keyword">if</span> (!ok) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        refs.put(key, <span class="number">1</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">unlock</span><span class="params">(String key)</span> </span>&#123;</span><br><span class="line">        Map&lt;String, Integer&gt; refs = currentLockers();</span><br><span class="line">        Integer refCnt = refs.get(key);</span><br><span class="line">        <span class="keyword">if</span> (refCnt == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        refCnt = refCnt - <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">if</span> ((refCnt &gt; <span class="number">0</span>)) &#123;</span><br><span class="line">            refs.put(key, refCnt);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            refs.remove(key);</span><br><span class="line">            <span class="keyword">this</span>._unlock(key);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目十四：NIO-demo-com-lvshen-demo-nio；"><a href="#项目十四：NIO-demo-com-lvshen-demo-nio；" class="headerlink" title="项目十四：NIO demo com.lvshen.demo.nio；"></a>项目十四：NIO demo <code>com.lvshen.demo.nio</code>；</h4><blockquote>
<p>见博客文章：<a href="https://lvshen9.gitee.io/2020/05/07/1/">BIO与NIO与多路复用</a></p>
</blockquote>
<h4 id="项目十五：Postfix-Completion-使用-com-lvshen-demo-postfixcompletion；"><a href="#项目十五：Postfix-Completion-使用-com-lvshen-demo-postfixcompletion；" class="headerlink" title="项目十五：Postfix Completion 使用 com.lvshen.demo.postfixcompletion；"></a>项目十五：Postfix Completion 使用 <code>com.lvshen.demo.postfixcompletion</code>；</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">    Student student = <span class="keyword">new</span> Student();<span class="comment">//.var</span></span><br><span class="line">    Main.student = <span class="keyword">new</span> Student();<span class="comment">// .filed 全局变量</span></span><br><span class="line">    Student student1 = <span class="keyword">new</span> Student(); <span class="comment">//Student.new</span></span><br><span class="line">    Student student2 = (Student) <span class="keyword">new</span> Object(); <span class="comment">//new Object().castvar</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (student == <span class="keyword">null</span>) &#123;  <span class="comment">//student.null</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (student != <span class="keyword">null</span>) &#123; <span class="comment">//student.notnull</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">boolean</span> flag = <span class="keyword">true</span>;</span><br><span class="line">    <span class="keyword">if</span> (flag) &#123;   <span class="comment">//flag.if</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (flag) &#123; <span class="comment">//flag.while</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    System.out.println(flag); <span class="comment">//flag.sout</span></span><br><span class="line">    <span class="comment">//return flag;     flag.return</span></span><br><span class="line"></span><br><span class="line">    String[] strs = <span class="keyword">new</span> String[<span class="number">5</span>];</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; strs.length; i++) &#123; <span class="comment">//strs.fori</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (String str : strs) &#123;  <span class="comment">//strs.for</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = strs.length - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--) &#123; <span class="comment">//strs.forr</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        main(<span class="keyword">new</span> String[]&#123;&#125;);  <span class="comment">//main.try</span></span><br><span class="line">    &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    List&lt;Member&gt; members =<span class="keyword">new</span> ArrayList&lt;&gt;(); <span class="comment">//Member.list</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目十六：手写Spring启动流程-com-lvshen-demo-spring；"><a href="#项目十六：手写Spring启动流程-com-lvshen-demo-spring；" class="headerlink" title="项目十六：手写Spring启动流程 com.lvshen.demo.spring；"></a>项目十六：手写Spring启动流程 <code>com.lvshen.demo.spring</code>；</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doGet</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.doPost(req, resp);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doPost</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        <span class="comment">//6.调用</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            doDispatch(req, resp);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            resp.getWriter().write(<span class="string">"500 Exception Detail "</span> + Arrays.toString(e.getStackTrace()));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">(ServletConfig config)</span> </span>&#123;</span><br><span class="line">    <span class="comment">//1.加载配置文件</span></span><br><span class="line">    doLoadConfig(config.getInitParameter(<span class="string">"contextConfigLocation"</span>));</span><br><span class="line"></span><br><span class="line">    <span class="comment">//2.扫描相关的类</span></span><br><span class="line">    doScanner(configProperties.getProperty(<span class="string">"scanPackage"</span>));</span><br><span class="line"></span><br><span class="line">    <span class="comment">//3.实例化相关的类</span></span><br><span class="line">    doInstance();</span><br><span class="line"></span><br><span class="line">    <span class="comment">//4.完成依赖注入</span></span><br><span class="line">    doAutowired();</span><br><span class="line"></span><br><span class="line">    <span class="comment">//5.初始化HandlerMapping</span></span><br><span class="line">    doInitHandlerMapping();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目十七：-SpringCloud-Alibaba使用-com-lvshen-demo-springcloudalibaba；"><a href="#项目十七：-SpringCloud-Alibaba使用-com-lvshen-demo-springcloudalibaba；" class="headerlink" title="项目十七： SpringCloud Alibaba使用 com.lvshen.demo.springcloudalibaba；"></a>项目十七： SpringCloud Alibaba使用 <code>com.lvshen.demo.springcloudalibaba</code>；</h4><h4 id="项目十八：面试题-com-lvshen-demo-test；"><a href="#项目十八：面试题-com-lvshen-demo-test；" class="headerlink" title="项目十八：面试题 com.lvshen.demo.test；"></a>项目十八：面试题 <code>com.lvshen.demo.test</code>；</h4><h5 id="阿里面试题"><a href="#阿里面试题" class="headerlink" title="阿里面试题"></a>阿里面试题</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">//评测题目: 三个线程A、B、C，实现一个程序让线程A打印“A”，线程B打印“B”，线程C打印“C”，</span><br><span class="line">//三个线程输出ABCABCABC......ABC，循环10次“ABC”</span><br><span class="line">//30分钟</span><br></pre></td></tr></table></figure>
<h5 id="华为面试题"><a href="#华为面试题" class="headerlink" title="华为面试题"></a>华为面试题</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">二分查找和冒泡排序</span><br></pre></td></tr></table></figure>
<h5 id="腾讯面试题"><a href="#腾讯面试题" class="headerlink" title="腾讯面试题"></a>腾讯面试题</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Description:给出有序数组(非递减)和闭区间, 找出数组中在区间之内的元素起始位置和结束位置</span><br><span class="line"> * 输入：</span><br><span class="line"> * 1. 有序数组[1,1,2,3,4,5,5]</span><br><span class="line"> * 2. 闭区间[-3,3]</span><br><span class="line"> * 输出：[0,3]</span><br><span class="line"> * 解释：在数组中，前4个元素在区间之内，则起始位置为0，结束位置为3</span><br><span class="line"> * 要求：最坏情况时间复杂度小于O(n)</span><br><span class="line"> *</span><br><span class="line"> * @author Lvshen</span><br><span class="line"> * @version 1.0</span><br><span class="line"> * @date: 2020/4/27 18:56</span><br><span class="line"> * @since JDK 1.8</span><br><span class="line"> */</span><br></pre></td></tr></table></figure>
<h4 id="项目十九：Thread多线程com-lvshen-demo-test；"><a href="#项目十九：Thread多线程com-lvshen-demo-test；" class="headerlink" title="项目十九：Thread多线程com.lvshen.demo.test；"></a>项目十九：Thread多线程<code>com.lvshen.demo.test</code>；</h4><h5 id="AQS"><a href="#AQS" class="headerlink" title="AQS"></a>AQS</h5><h6 id="Semaphone"><a href="#Semaphone" class="headerlink" title="Semaphone"></a><code>Semaphone</code></h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SemaphoreDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        SemaphoreDemo semaphoreDemo = <span class="keyword">new</span> SemaphoreDemo();</span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">9</span>;    <span class="comment">//数量</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//循环屏障</span></span><br><span class="line">        CyclicBarrier cyclicBarrier = <span class="keyword">new</span> CyclicBarrier(count);</span><br><span class="line"></span><br><span class="line">        Semaphore semaphore = <span class="keyword">new</span> Semaphore(<span class="number">5</span>);<span class="comment">//限制请求数量</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; count; i++) &#123;</span><br><span class="line">            String vipNo = <span class="string">"vip-00"</span> + i;</span><br><span class="line">            <span class="keyword">new</span> Thread(() -&gt; &#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    cyclicBarrier.await();</span><br><span class="line">                    <span class="comment">//semaphore.acquire();//获取令牌</span></span><br><span class="line">                    <span class="keyword">boolean</span> tryAcquire = semaphore.tryAcquire(<span class="number">3000L</span>, TimeUnit.MILLISECONDS);</span><br><span class="line">                    <span class="keyword">if</span> (!tryAcquire) &#123;</span><br><span class="line">                        System.out.println(<span class="string">"获取令牌失败："</span> + vipNo);</span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                    <span class="comment">//执行操作逻辑</span></span><br><span class="line">                    System.out.println(<span class="string">"当前线程："</span> + Thread.currentThread().getName());</span><br><span class="line">                    semaphoreDemo.service(vipNo);</span><br><span class="line">                &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">                    semaphore.release();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;).start();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 限流控制5个线程同时访问</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">service</span><span class="params">(String vipNo)</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"楼上出来迎接贵宾一位，贵宾编号"</span> + vipNo + <span class="string">",..."</span>);</span><br><span class="line">        Thread.sleep(<span class="keyword">new</span> Random().nextInt(<span class="number">3000</span>));</span><br><span class="line">        System.out.println(<span class="string">"欢送贵宾出门，贵宾编号"</span> + vipNo);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h6 id="CountDownLatch"><a href="#CountDownLatch" class="headerlink" title="CountDownLatch"></a><code>CountDownLatch</code></h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CountDownLatchDemo</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> COUNT = <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> CountDownLatch cdl = <span class="keyword">new</span> CountDownLatch(COUNT);</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="keyword">new</span> Thread(<span class="keyword">new</span> Teacher(cdl)).start();</span><br><span class="line">        Thread.sleep(<span class="number">1</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; COUNT; i++) &#123;</span><br><span class="line">            <span class="keyword">new</span> Thread(<span class="keyword">new</span> Student(i, cdl)).start();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">synchronized</span> (CountDownLatchDemo<span class="class">.<span class="keyword">class</span>) </span>&#123;</span><br><span class="line">            CountDownLatchDemo<span class="class">.<span class="keyword">class</span>.<span class="title">wait</span>()</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Teacher</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        CountDownLatch cdl;</span><br><span class="line"></span><br><span class="line">        Teacher(CountDownLatch cdl) &#123;</span><br><span class="line">            <span class="keyword">this</span>.cdl = cdl;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            System.out.println(<span class="string">"老师发卷子。。。"</span>);</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                cdl.await();</span><br><span class="line">            &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(<span class="string">"老师收卷子。。。"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        CountDownLatch cdl;</span><br><span class="line">        <span class="keyword">int</span> num;</span><br><span class="line"></span><br><span class="line">        Student(<span class="keyword">int</span> num, CountDownLatch cdl) &#123;</span><br><span class="line">            <span class="keyword">this</span>.num = num;</span><br><span class="line">            <span class="keyword">this</span>.cdl = cdl;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            System.out.println(String.format(<span class="string">"学生(%s)写卷子。。。"</span>,num));</span><br><span class="line">            <span class="comment">//doingLongTime();</span></span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(String.format(<span class="string">"学生(%s)写卷子。。。"</span>,num));</span><br><span class="line">            cdl.countDown();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h6 id="CyclicBarrier"><a href="#CyclicBarrier" class="headerlink" title="CyclicBarrier"></a><code>CyclicBarrier</code></h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CyclicBarrierDemo</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> COUNT = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; COUNT; i++) &#123;</span><br><span class="line">            <span class="keyword">new</span> Thread(<span class="keyword">new</span> Staff(i, cb)).start();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">synchronized</span> (CyclicBarrierDemo<span class="class">.<span class="keyword">class</span>) </span>&#123;</span><br><span class="line">            CyclicBarrierDemo<span class="class">.<span class="keyword">class</span>.<span class="title">wait</span>()</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> CyclicBarrier cb = <span class="keyword">new</span> CyclicBarrier(COUNT, <span class="keyword">new</span> Singer());</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Singer</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            System.out.println(<span class="string">"为大家唱歌。。。"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Staff</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        CyclicBarrier cb;</span><br><span class="line">        <span class="keyword">int</span> num;</span><br><span class="line"></span><br><span class="line">        Staff(<span class="keyword">int</span> num, CyclicBarrier cb) &#123;</span><br><span class="line">            <span class="keyword">this</span>.num = num;</span><br><span class="line">            <span class="keyword">this</span>.cb = cb;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@SneakyThrows</span></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            System.out.println(String.format(<span class="string">"员工(%s)出发。。。"</span>, num));</span><br><span class="line">            Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">            System.out.println(String.format(<span class="string">"员工(%s)到达地点一。。。"</span>, num));</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                cb.await();</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(String.format(<span class="string">"员工(%s)再出发。。。"</span>, num));</span><br><span class="line">            Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">            <span class="comment">//doingLongTime();</span></span><br><span class="line">            System.out.println(String.format(<span class="string">"员工(%s)到达地点二。。。"</span>, num));</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                cb.await();</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(String.format(<span class="string">"员工(%s)再出发。。。"</span>, num));</span><br><span class="line">            Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">            <span class="comment">//doingLongTime();</span></span><br><span class="line">            System.out.println(String.format(<span class="string">"员工(%s)到达地点三。。。"</span>, num));</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                cb.await();</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(String.format(<span class="string">"员工(%s)结束。。。"</span>, num));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="线程状态"><a href="#线程状态" class="headerlink" title="线程状态"></a>线程状态</h5><h5 id="sleep"><a href="#sleep" class="headerlink" title="sleep"></a>sleep</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sleep -&gt; TIMED_WAITING</span><br></pre></td></tr></table></figure>
<h5 id="join"><a href="#join" class="headerlink" title="join"></a>join</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">t2中执行t1.join(5000L)</span><br><span class="line">t2的状态：TIMED_WAITING</span><br><span class="line">t2中执行t1.join()</span><br><span class="line">t2的状态：WAITING</span><br></pre></td></tr></table></figure>
<h5 id="synchronized"><a href="#synchronized" class="headerlink" title="synchronized"></a>synchronized</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">t1抢不到锁的状态：BLOCKED</span><br></pre></td></tr></table></figure>
<h5 id="wait"><a href="#wait" class="headerlink" title="wait"></a>wait</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">WATTING || BLOCKING</span><br></pre></td></tr></table></figure>
<h5 id="park"><a href="#park" class="headerlink" title="park"></a>park</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">t1 park后的状态：WAITING</span><br><span class="line">t1 unpark后的状态：WAITING</span><br></pre></td></tr></table></figure>
<h5 id="手写future类"><a href="#手写future类" class="headerlink" title="手写future类"></a>手写future类</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyFutureTask</span>&lt;<span class="title">T</span>&gt; <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    Callable&lt;T&gt; callable;</span><br><span class="line"></span><br><span class="line">    T result;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">volatile</span> String state = <span class="string">"NEW"</span>;</span><br><span class="line"></span><br><span class="line">    LinkedBlockingQueue&lt;Thread&gt; queue = <span class="keyword">new</span> LinkedBlockingQueue&lt;Thread&gt;();</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">MyFutureTask</span><span class="params">(Callable&lt;T&gt; callable)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.callable = callable;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> T <span class="title">get</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="string">"END"</span>.equals(state)) &#123;</span><br><span class="line">            <span class="keyword">return</span> result;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (!<span class="string">"END"</span>.equals(state)) &#123;</span><br><span class="line">            queue.add(Thread.currentThread());</span><br><span class="line">            LockSupport.park();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            result = callable.call();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            state = <span class="string">"END"</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        Thread th = queue.poll();</span><br><span class="line">        <span class="keyword">if</span> (queue != <span class="keyword">null</span>) &#123;</span><br><span class="line">            LockSupport.unpark(th);</span><br><span class="line">            th = queue.poll();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="LockSupport使用"><a href="#LockSupport使用" class="headerlink" title="LockSupport使用"></a>LockSupport使用</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LockSupportDemo</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">static</span> Thread t1, t2, t3;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        t1 = <span class="keyword">new</span> Thread(() -&gt; &#123;</span><br><span class="line">            <span class="keyword">while</span> (i &lt; <span class="number">10</span>) &#123;</span><br><span class="line">                System.out.println(<span class="string">"t1:"</span> + (++i));</span><br><span class="line">                LockSupport.unpark(t2);     <span class="comment">//t2变为非阻塞</span></span><br><span class="line">                LockSupport.park();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        t2 = <span class="keyword">new</span> Thread(() -&gt; &#123;</span><br><span class="line">            <span class="keyword">while</span> (i &lt; <span class="number">10</span>) &#123;</span><br><span class="line">                System.out.println(<span class="string">"   t2:"</span> + (++i));</span><br><span class="line">                LockSupport.unpark(t3);</span><br><span class="line">                LockSupport.park();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        t3 = <span class="keyword">new</span> Thread(() -&gt; &#123;</span><br><span class="line">            <span class="keyword">while</span> (i &lt; <span class="number">10</span>) &#123;</span><br><span class="line">                System.out.println(<span class="string">"t3:"</span> + (++i));</span><br><span class="line">                LockSupport.unpark(t1);</span><br><span class="line">                LockSupport.park();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        t1.start();</span><br><span class="line">        t2.start();</span><br><span class="line">        t3.start();</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目二十：-本地线程-com-lvshen-demo-threadlocal；"><a href="#项目二十：-本地线程-com-lvshen-demo-threadlocal；" class="headerlink" title="项目二十： 本地线程 com.lvshen.demo.threadlocal；"></a>项目二十： 本地线程 <code>com.lvshen.demo.threadlocal</code>；</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ThreadLocalTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">private</span> <span class="keyword">static</span> ThreadLocal&lt;Student&gt; threadLocal = <span class="keyword">new</span> ThreadLocal&lt;&gt;();</span><br><span class="line"></span><br><span class="line">   <span class="meta">@Test</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span> </span>&#123;</span><br><span class="line">      Student student1 = <span class="keyword">new</span> Student(<span class="number">11</span>, <span class="string">"lvshen"</span>);</span><br><span class="line">      Student student2 = <span class="keyword">new</span> Student(<span class="number">22</span>, <span class="string">"niumo"</span>);</span><br><span class="line"></span><br><span class="line">      threadLocal.set(student1);</span><br><span class="line">      threadLocal.set(student2);</span><br><span class="line"></span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="meta">@Test</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">      printAllSlot(<span class="number">16</span>);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">printAllSlot</span><span class="params">(<span class="keyword">int</span> len)</span> </span>&#123;</span><br><span class="line">      System.out.println(<span class="string">"************* len = "</span> + len + <span class="string">" ***************"</span>);</span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= <span class="number">64</span>; i++) &#123;</span><br><span class="line">         ThreadLocal&lt;String&gt; local = <span class="keyword">new</span> ThreadLocal&lt;&gt;();</span><br><span class="line">         <span class="keyword">int</span> slot = getSlot(local, len);</span><br><span class="line">         System.out.print(slot + <span class="string">" "</span>);</span><br><span class="line">         <span class="keyword">if</span> (i % len == <span class="number">0</span>) &#123;</span><br><span class="line">            System.out.println();</span><br><span class="line">         &#125;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="function"><span class="keyword">static</span> <span class="keyword">int</span> <span class="title">getSlot</span><span class="params">(ThreadLocal&lt;?&gt; t, <span class="keyword">int</span> len)</span> </span>&#123;</span><br><span class="line">      <span class="keyword">int</span> hashCode = getHashCode(t);</span><br><span class="line">      <span class="keyword">return</span> hashCode &amp; (len - <span class="number">1</span>);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="function"><span class="keyword">static</span> <span class="keyword">int</span> <span class="title">getHashCode</span><span class="params">(ThreadLocal&lt;?&gt; t)</span> </span>&#123;</span><br><span class="line">      Field field;</span><br><span class="line">      <span class="keyword">try</span> &#123;</span><br><span class="line">         field = t.getClass().getDeclaredField(<span class="string">"threadLocalHashCode"</span>);</span><br><span class="line">         field.setAccessible(<span class="keyword">true</span>);</span><br><span class="line">         <span class="keyword">return</span> (<span class="keyword">int</span>) field.get(t);</span><br><span class="line">      &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">         e.printStackTrace();</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目二十一：手写线程池-com-lvshen-demo-threadpool；"><a href="#项目二十一：手写线程池-com-lvshen-demo-threadpool；" class="headerlink" title="项目二十一：手写线程池 com.lvshen.demo.threadpool；"></a>项目二十一：手写线程池 <code>com.lvshen.demo.threadpool</code>；</h4><blockquote>
<p><a href="https://lvshen9.gitee.io/2020/04/17/1/">手写线程池</a></p>
</blockquote>
<h4 id="项目二十二：递归实现树状结构-com-lvshen-demo-treenode；"><a href="#项目二十二：递归实现树状结构-com-lvshen-demo-treenode；" class="headerlink" title="项目二十二：递归实现树状结构 com.lvshen.demo.treenode；"></a>项目二十二：递归实现树状结构 <code>com.lvshen.demo.treenode</code>；</h4><h4 id="项目二十三：Java8使用-com-lvshen-demo-yjh-java8；"><a href="#项目二十三：Java8使用-com-lvshen-demo-yjh-java8；" class="headerlink" title="项目二十三：Java8使用 com.lvshen.demo.yjh.java8；"></a>项目二十三：Java8使用 <code>com.lvshen.demo.yjh.java8</code>；</h4><blockquote>
<p>见博客：<a href="https://lvshen9.gitee.io/2019/08/16/1/">我所用到的Java8</a></p>
</blockquote>
<p>LongAddr使用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> LongAdder longAdderVal = <span class="keyword">new</span> LongAdder();</span><br><span class="line">longAdderVal.increment();</span><br></pre></td></tr></table></figure>
<p>Lambda表达式，foreach</p>
<p>LocalDate</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLocalDate</span><span class="params">()</span> </span>&#123;</span><br><span class="line">   LocalDate localDate = LocalDate.now();</span><br><span class="line">   LocalTime localTime = LocalTime.of(<span class="number">12</span>, <span class="number">20</span>);</span><br><span class="line">   LocalDateTime localDateTime = LocalDateTime.now();</span><br><span class="line">   OffsetDateTime offsetDateTime = OffsetDateTime.now();</span><br><span class="line">   ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of(<span class="string">"Europe/Paris"</span>));</span><br><span class="line">   System.out.println(<span class="string">"program execute Successful!!!"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目二十四：Zookeeper监听机制使用-com-lvshen-demo-zookeeperdemo；"><a href="#项目二十四：Zookeeper监听机制使用-com-lvshen-demo-zookeeperdemo；" class="headerlink" title="项目二十四：Zookeeper监听机制使用 com.lvshen.demo.zookeeperdemo；"></a>项目二十四：Zookeeper监听机制使用 <code>com.lvshen.demo.zookeeperdemo</code>；</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Slf</span>4j</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WatcherApi</span> <span class="keyword">implements</span> <span class="title">Watcher</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">process</span><span class="params">(WatchedEvent watchedEvent)</span> </span>&#123;</span><br><span class="line">        log.info(<span class="string">"【Watcher监听事件】=&#123;&#125;"</span>,watchedEvent.getState());</span><br><span class="line">        log.info(<span class="string">"【监听路径为】=&#123;&#125;"</span>,watchedEvent.getPath());</span><br><span class="line">        log.info(<span class="string">"【监听的类型为】=&#123;&#125;"</span>,watchedEvent.getType()); <span class="comment">//  三种监听类型： 创建，删除，更新</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 测试方法  初始化</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@PostConstruct</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        String path = <span class="string">"/test/lvshen"</span>;</span><br><span class="line">        log.info(<span class="string">"【执行初始化测试方法。。。。。。。。。。。。】"</span>);</span><br><span class="line">        <span class="comment">//createNode(path, "测试");</span></span><br><span class="line">        String value = getData(path, <span class="keyword">new</span> WatcherApi());</span><br><span class="line">        log.info(<span class="string">"【执行初始化测试方法getData返回值。。。。。。。。。。。。】=&#123;&#125;"</span>, value);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 删除节点出发 监听事件</span></span><br><span class="line">        <span class="comment">//deleteNode(path);</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@Bean</span>(name = <span class="string">"zkClient"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> ZooKeeper <span class="title">zkClient</span><span class="params">()</span></span>&#123;</span><br><span class="line">        ZooKeeper zooKeeper=<span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">final</span> CountDownLatch countDownLatch = <span class="keyword">new</span> CountDownLatch(<span class="number">1</span>);</span><br><span class="line">            <span class="comment">//连接成功后，会回调watcher监听，此连接操作是异步的，执行完new语句后，直接调用后续代码</span></span><br><span class="line">            <span class="comment">//  可指定多台服务地址 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183</span></span><br><span class="line">            zooKeeper = <span class="keyword">new</span> ZooKeeper(connectString, timeout, <span class="keyword">new</span> Watcher() &#123;</span><br><span class="line">                <span class="meta">@Override</span></span><br><span class="line">                <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">process</span><span class="params">(WatchedEvent event)</span> </span>&#123;</span><br><span class="line">                    <span class="keyword">if</span>(Event.KeeperState.SyncConnected==event.getState())&#123;</span><br><span class="line">                        <span class="comment">//如果收到了服务端的响应事件,连接成功</span></span><br><span class="line">                        countDownLatch.countDown();</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;);</span><br><span class="line">            countDownLatch.await();</span><br><span class="line">            log.info(<span class="string">"【初始化ZooKeeper连接状态....】=&#123;&#125;"</span>,zooKeeper.getState());</span><br><span class="line"></span><br><span class="line">        &#125;<span class="keyword">catch</span> (Exception e)&#123;</span><br><span class="line">            log.error(<span class="string">"初始化ZooKeeper连接异常....】=&#123;&#125;"</span>,e);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span>  zooKeeper;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h4 id="项目二十五：测试类-test-java-com-lvshen-demo；"><a href="#项目二十五：测试类-test-java-com-lvshen-demo；" class="headerlink" title="项目二十五：测试类 test.java.com.lvshen.demo；"></a>项目二十五：测试类 <code>test.java.com.lvshen.demo</code>；</h4><p>相关功能测试</p>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-05-26T05:46:08.926Z" itemprop="dateUpdated">2020-05-26 13:46:08</time>
</span><br>


        
        原文链接：<a href="/2020/05/24/1/" target="_blank" rel="external">https://lvshen9.gitee.io/2020/05/24/1/</a>
        
    </div>
    
    <footer>
        <a href="https://lvshen9.gitee.io">
            <img src="/img/avatar.jpg" alt="我的技术小房间">
            我的技术小房间
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Java/">Java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/github/">github</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lvshen9.gitee.io/2020/05/24/1/&title=《我在github上的Java项目》 — Lvshen's Blog&pic=https://lvshen9.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lvshen9.gitee.io/2020/05/24/1/&title=《我在github上的Java项目》 — Lvshen's Blog&source=
github地址：Lvshen项目-Demo[Java技术栈]

模块一：自定义注解com.lvshen.demo.annotation注解定义1234..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lvshen9.gitee.io/2020/05/24/1/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《我在github上的Java项目》 — Lvshen's Blog&url=https://lvshen9.gitee.io/2020/05/24/1/&via=https://lvshen9.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lvshen9.gitee.io/2020/05/24/1/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2020/07/01/1/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">后端开发面试分享</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2020/05/07/1/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">BIO与NIO与多路复用</h4>
      </a>
    </div>
  
</nav>



    











    <!-- Valine Comments -->
    <div class="comments vcomment" id="comments"></div>
    <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
    <script src="//unpkg.com/valine@latest/dist/Valine.min.js"></script>
    <!-- Valine Comments script -->
    <script>
        var GUEST_INFO = ['nick','mail','link'];
        var guest_info = 'nick,mail,link'.split(',').filter(function(item){
          return GUEST_INFO.indexOf(item) > -1
        });
        new Valine({
            el: '#comments',
            notify: 'false' == 'true',
            verify: 'false' == 'true',
            appId: "dy9kXHwg5jQUlLryQmpjWRlM-gzGzoHsz",
            appKey: "P9Nh39Ol0JbMMiYqNGHEP3ml",
            avatar: "mm",
            placeholder: "Just go go",
            guest_info: guest_info.length == 0 ? GUEST_INFO : guest_info,
            pageSize: "10"
        })
    </script>
    <!-- Valine Comments end -->







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="https://lvshen9.github.io/blog2/pay/weixin.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="https://lvshen9.github.io/blog2/pay/weixin.jpg" data-alipay="https://lvshen9.github.io/blog2/pay/zhifu.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>我的技术小房间 &copy; 2015 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lvshen9.gitee.io/2020/05/24/1/&title=《我在github上的Java项目》 — Lvshen's Blog&pic=https://lvshen9.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lvshen9.gitee.io/2020/05/24/1/&title=《我在github上的Java项目》 — Lvshen's Blog&source=
github地址：Lvshen项目-Demo[Java技术栈]

模块一：自定义注解com.lvshen.demo.annotation注解定义1234..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lvshen9.gitee.io/2020/05/24/1/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《我在github上的Java项目》 — Lvshen's Blog&url=https://lvshen9.gitee.io/2020/05/24/1/&via=https://lvshen9.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lvshen9.gitee.io/2020/05/24/1/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=https://lvshen9.gitee.io/2020/05/24/1/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
